package com.RuiJi.controller;

import com.RuiJi.entity.Category;
import com.RuiJi.entity.Dish;
import com.RuiJi.service.CategoryService;
import com.RuiJi.service.DishService;
import com.RuiJi.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品分类
 *
 * @author Twinkle
 * @PACKAGE_NAME: com.RuiJi.controller
 * @NAME: CategoryController
 * @USER: Twinkle
 * @DATE: 2023/4/7
 * @TIME: 16:40
 * @YEAR: 2023
 * @MONTH: 04
 */
@Slf4j
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增菜品
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info("新增{}", category);
        categoryService.save(category);
        return R.success("新增成功");
    }

    /**
     * 分页展示
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {

        Page<Category> pageinfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageinfo, queryWrapper);
        return R.success(pageinfo);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        /*categoryService.removeById(id);*/
        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }

    /**
     * 修改操作
     * @param category
     * @return
     */

    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功");
    }
        /**
         * @description: 查询
         * @author: fanli
         * @date: 2023/4/18 19:32
         * @param: [category]
         * @return:
         **/
    @GetMapping("/list")
    public R<List<Category>> list(Category category){

        LambdaQueryWrapper<Category>queryWrapper=new  LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);

        return R.success(list);
    }



}
